Test Annotations | BeforeSuite | AfterSuite
BeforeSuite
- @BeforeSuite: This annotation marks a method that will run before any test in the entire suite. It’s typically used for tasks like initializing resources that need to be available across multiple test classes (e.g., starting a server, loading configuration files, etc.).
AfterSuite
- @AfterSuite: This annotation marks a method that will run after all the tests in the suite have finished. It's useful for cleaning up resources used throughout the entire suite, like shutting down a server or database connection.
Steps to test Before suite and After Suite
First create a new package and new Class
- for example : package name is common and class name is commonDataSetUp and write the code in that class as given below
package common;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
public class commonDataSetUp {
@BeforeSuite
public static void dataSetup() {
System.out.println("Common Data SetUp");
}
@AfterSuite
public static void dataTearDown() {
System.out.println("Common data CleanUp");
}
}
Create a new class in a previous package and write below code
package asc;
import org.testng.annotations.Test;
import common.commonDataSetUp;
public class groupTest2 extends commonDataSetUp {
@Test(priority = 1,groups="regression")
public void Test1()
{
System.out.println("Test 1");
}
@Test(priority = 2,groups="regression")
public void Test2() {
System.out.println("Test 2");
}
@Test(groups={"regression","bvt"})
public void Test3() {
System.out.println("Test 3");
}
@Test(groups="bvt")
public void Test4() {
System.out.println("Test 4");
}
}
And then Create a New test Suite Name testng1 and change the code in that xml file as given below
< ?xml version="1.0" encoding="UTF-8"?>
< !DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
< suite name="Suite Setup and teardown">
< test name="Test Before Suite After Suite">
< classes>
< class name="asc.groupTest2"/>
< /classes>
< /test>
< /suite>
And then Run this code